#include "head.h"

//断开数据库链接
void close_connection(MYSQL *mysql){
    mysql_close(mysql);
} 

/* 连接mysql */
void mysqldb_connect(MYSQL *mysql)
{
    if(!mysql_real_connect(mysql, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, DB_NAME, 0, NULL, 0)) {
        printf("\nFailed to connect:%s\n", mysql_error(mysql));
    } else {
       printf("\nConnect sucessfully!\n");
    }
}

void mysqldb_insert(MYSQL *mysql,  char *sql_insert)
{

    int t;//判断查询是否正确
  /*  char *head = "INSERT INTO ";// 插入命令
    //char query[120];//最终的查询语句
    char query[300];//最终的查询语句
    char field[48] = "user_name, user_password, user_salt";//查询的属性
    char *left = "(";  //拼接左括号
    char *right = ") ";  //拼接右括号
    char *values = "VALUES";  //插入命令
    char message[100] = {0};  //插入的值


    //sprintf(message, "%d, %s, \"%s\", %d", user_id, user_name, user_password, user_salt);// 传入的参
    sprintf(message, "\"%s\", \"%s\", \"%s\" ", user_name, user_password, user_salt);// 传入的参数进行

    printf("message: %s \n", message);

    // 拼接sql命令
    sprintf(query, "%s%s%s%s%s%s%s%s%s", head, TABLE_NAME, left, field, right, values, left, message,
    printf("%s\n", query);  //打印拼接命令
 */
    char *query = sql_insert;
    t = mysql_real_query(mysql, query, strlen(query));//将数据传入到数据库中如果成功返回0，失败返回1
    if (t) {
        printf("Failed to query: %s\n", mysql_error(mysql));
    }
    else {
        printf("\nInsert sucessfully!\n");
    }

}

/* 查询数据 */
char* mysqldb_query(MYSQL *mysql, char *has)//
{

    MYSQL *mysql_user = mysql_init(NULL);
    if (!mysql_user) {
        printf("\nmysql_user init failed.\n");
    }
    MYSQL *mysql_file = mysql_init(NULL);
    if (!mysql_file) {
        printf("\nmysql_file init failed.\n");
    }

    if(!mysql_real_connect(mysql_user, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, DB_NAME, 0, NULL, 0)) {
        printf("\nFailed to connect:%s\n", mysql_error(mysql));
    } else {
       printf("\nConnect sucessfully!\n");
    }
    if(!mysql_real_connect(mysql_file, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, DB_NAME, 0, NULL, 0)) {
        printf("\nFailed to connect:%s\n", mysql_error(mysql));
    } else {
       printf("\nConnect sucessfully!\n");
    }


    //链接数据库
    //mysqldb_connect(mysql_user);
    //mysqldb_connect(mysql_file);

    int t_user;//判断 t_user查询是否正确
    int t_file; //t_file查询是否正确
    char *head = "SELECT * FROM ";//语句拼接
    char query_user[50] = {0}; //sql_user指令缓冲区
    char query_file[50] = {0};//sql_file指令缓冲区
    MYSQL_RES *res_user;// 获取由mysql_user_query()函数返回的结果
    MYSQL_RES *res_file;// 获取由mysql_file_query()函数返回的结果
    MYSQL_ROW row_user; // 从user结果当中取出一行
    MYSQL_ROW row_file; // 从file结果当中取出一行
    char* table_user = "user";
    char *table_file = "file_table";

    //sprintf(query, "%s%s", head, TABLE_NAME);
    sprintf(query_user, "%s%s", head, table_user);
    sprintf(query_file, "%s%s", head, table_file);
    t_user = mysql_real_query(mysql_user, query_user, strlen(query_user));
    t_file = mysql_real_query(mysql_file, query_file, strlen(query_file));
/*
    if (t_user) {
        printf("t_user Failed to query: %s\n", mysql_error(mysql_user));
        return NULL;
    } else {
        printf("\nt_user Query successfully!\n");
    }
    if(t_file){
        printf("t_file Failed to query: %s\n", mysql_error(mysql_file));
        return NULL;
    }else{
        printf("\nt_file Query successfully!\n");
    }
 */
    res_user = mysql_store_result(mysql_user);//获取sql_user查询出来的结果

    res_file = mysql_store_result(mysql_file);//获取sql_file查询出来的结果

    int user_id = 0;//用户id号
    int flag_user = 1;//标记是否用户存在 0存在， 1不存在
    while (row_user = mysql_fetch_row(res_user)) {//进行遍历输出
         user_id++;
        for(t_user = 0; t_user < mysql_num_fields(res_user); t_user++) {
            //printf("%s\t", row_user[t_user]);
            //进行判断如果密码的hash值是否存在
            if(strcmp(has, row_user[t_user]) == 0){
            //  存在成功,记录用户的id号
              flag_user = 0;
              break;
            }
       // printf("\n");
         }
        if(flag_user == 0){
            break;
        }
    }
  //  printf("user_id %d \n", user_id);
    //判断用户是否存在
    if(flag_user){
        //  不成功提示密码输入错误
        //printf("flag_user: %d \n", flag_user);
        char * str_tip = "用户账号或者密码错误";
        return str_tip;
    }
    char *str_sql_all = (char*)malloc(4096);
    //char str_sql_all[4096] = "";
    while (row_file = mysql_fetch_row(res_file)) {//进行遍历输出file
         for(t_file = 0; t_file < mysql_num_fields(res_file); t_file++) {
            // printf("%s\t", row_file[t_file]);
             int file_user_id = atoi(row_file[7]);
           //  printf("file_user_id= %d \n", file_user_id);
             if(file_user_id == user_id){
                 strcat(str_sql_all, row_file[t_file]);
                 strcat(str_sql_all, " ");
             }
          //  printf("str_sql_all: %s \n", str_sql_all);
         }
        // printf("\n");
    }
   // strcat(str_sql_all, ",,,,,,,");
   // printf("199--------------------\n");
   // printf("str_sql_all: %s \n", str_sql_all);
    mysql_free_result(res_user);
    mysql_free_result(res_file);

    //close_connection(mysql_user);
    //close_connection(mysql_file);
    mysql_close(mysql_user);
    mysql_close(mysql_file);
    //*has = *str_sql_all;
    return str_sql_all;
//记得free()下
}
